<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Default_Model_User extends ZF_DB{
    protected $_table_name = "user" ;
    
    protected $_id;
    protected $_username;
    protected $_pw;
    protected $_fullname;
    protected $_displayname;
    protected $_telephone;
    protected $_email;
    protected $_type;
    protected $_address;
    protected $_city;
    protected $_nation;
    
    public function getId(){
        return $this->_id;
    }
    public function setId($id){
        $this->_id = $id;
    }
    public function getUsername(){
        return $this->_username;
    }
    public function setPw($pw){
        $this->_pw = $pw;
    }
    public function getPw(){
        return $this->_pw;
        
    }
    public function setFullname($fullname){
        $this->_fullname = $fullname;
    }
    public function getFullname(){
        return $this->_fullname;
    }
    public function setDisplayname($displayname){
        $this->_displayname = $displayname;
    }
    public function getDisplayname(){
        return $this->_displayname;
    }
    public function setTelephone($telephone){
        $this->_telephone = $telephone;
    }
    public function getTelephone(){
        return $this->_telephone;
        
    }
    public function setEmail($email){
        $this->_email = $email;
    }
    public function getEmail(){
        return $this->_email;
    }
    public function setType($type){
        $this->_type = $type;
    }
    public function getType(){
        return $this->_type;
    }
    public function setAddress($address){
        $this->_address = $address;
    }
    public function getAddress(){
        return $this->_address;
    }
    public function setCity($city){
        $this->_city = $city;
    }
    public function getCity(){
        return $this->_city;
    }
    public function setNation($nation){
        $this->_nation = $nation;
    }
    public function getNation(){
        return $this->_nation;
    }
   
    public function insert($username, $pw, $fullname, $displayname, $telephone, $email, $address, $city = 1, $nation = 1, $type = 1){
        if($username && $pw && $fullname && $displayname
                && $telephone && $email && $address
                && $city && $nation){             
                $data =  array(
                    'username' =>$username,
                    'pw'=>md5($pw),
                    'fullname' =>$fullname,
                    'displayname' =>$displayname,
                    'telephone'=>$telephone,
                    'email'=>$email,
                    'type' => $type,
                    'address'=>$address,
                    'city'=>$city,
                    'nation'=>$nation);
            return  $this->db->insert($this->_table_name,$data);
	}
        return -1;
	
    }
   
    public function update(){
        $where  = "id = $this->_id";
	$data =  array(
                    'username' =>$this->_username,
                    'pw'=>$this->_pw,
                    'fullname' =>$this->_fullname,
                    'displayname'=>$this->_displayname,
                    'telephone'=>$this->_telephone,
                    'email'=>$this->_email,
                    'type'=>$this->_type,
                    'address'=>$this->_address,
                    'city'=>$this->_city,
                    'nation' =>$this->_nation);
        return $this->db->update(
                $this->_table_name,
                $data,$where);
    }
    public function getByUsername($username){
        if($username)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.username = ?",$username );
		
		$data = $this->db->fetchRow($select);
		if($data){
			$this->_id = $data['id'];
			$this->_username = $data['username'];
			$this->_pw = $data['pw'];
			$this->_fullname = $data['fullname'];
                        $this->_displayname = $data['displayname'];       
                        $this->_telephone = $data['telephone'];
                        $this->_email = $data['email'];
                        $this->_type = $data['type'];
                        $this->_address = $data['address'];
                        $this->_city = $data['city'];
                        $this->_nation = $data['nation'];
                }
                return $data;
	}
	return -1;
    }
    
    public function getByuserId($id){
        if($id)
	{
		$select = $this->db->select()
			->from(array("p"=>$this->_table_name))
                        ->where("p.id = ?",$id );
		
		$data = $this->db->fetchRow($select);
		
                return $data;
	}
	return -1;
    }
}
?>
